import { ref, computed } from 'vue'
import { defineStore } from 'pinia'

export const useAuthStore = defineStore('auth', () => {
  const auth = ref<{
    XToken: string
    config: AppConfigModel
  }>({
    XToken: '',
    config: {
      theme: 'light',
      language: 'zh-CN',
      layout: 'basic',
      aside: 'left',
      console: 'right',
      typography: 'right',
      preferences: 'right',
    },
  })
  const gettersXToken = computed(() => auth.value.XToken)
  const updateXToken = (XToken: string) => {
    auth.value.XToken = XToken
    if (!XToken) {
      localStorage.removeItem('XToken')
      sessionStorage.removeItem('XToken')
    } else {
      localStorage.setItem('XToken', XToken)
      sessionStorage.setItem('XToken', XToken)
    }
  }
  const gettersConfig = computed(() => auth.value.config)
  const updateConfig = (config: AppConfigModel) => {
    auth.value.config = config
    localStorage.setItem('config', JSON.stringify(config))
  }
  return {
    gettersXToken,
    updateXToken,
    gettersConfig,
    updateConfig,
  }
})
